<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="kissdata">


    <meta name="subtitle" content="人生笔记">


    <meta name="description" content="南邮计算机学院2021毕业生王鹏的介绍，cs软件工程专业">


    <meta name="keywords" content="wangpeng 王鹏 南邮 南京">


<title>⛵ 各种环境都要能用 Git | 王鹏的小岛</title>



    <link rel="icon" href="/myIcon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        <!-- MathJax配置，可通过单美元符号书写行内公式等 -->
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
    "HTML-CSS": {
        preferredFont: "TeX",
        availableFonts: ["STIX","TeX"],
        linebreaks: { automatic:true },
        EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50)
    },
    tex2jax: {
        inlineMath: [ ["$", "$"], ["\\(","\\)"] ],
        processEscapes: true,
        ignoreClass: "tex2jax_ignore|dno",
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
        equationNumbers: { autoNumber: "AMS" },
        noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } },
        Macros: { href: "{}" }
    },
    messageStyle: "none"
    });
</script>
<!-- 给MathJax元素添加has-jax class -->
<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>
<!-- 通过连接CDN加载MathJax的js代码 -->
<script type="text/javascript" async
        src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML">
</script>


    


<meta name="generator" content="Hexo 7.0.0"></head>

<body>
    <script>
        // this function is used to check current theme before page loaded.
        (() => {
            const currentTheme = window.localStorage && window.localStorage.getItem('theme') || '';
            const isDark = currentTheme === 'dark';
            const pagebody = document.getElementsByTagName('body')[0]
            if (isDark) {
                pagebody.classList.add('dark-theme');
                // mobile
                document.getElementById("mobile-toggle-theme").innerText = "· Dark"
            } else {
                pagebody.classList.remove('dark-theme');
                // mobile
                document.getElementById("mobile-toggle-theme").innerText = "· Light"
            }
        })();
    </script>

    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">侠之大者</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/update/mystate">Career</a>
                
                    <a class="menu-item" href="/category">Legend</a>
                
                    <a class="menu-item" href="/about">Me</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">侠之大者</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/update/mystate">Career</a>
                
                    <a class="menu-item" href="/category">Legend</a>
                
                    <a class="menu-item" href="/about">Me</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
            <div class="main">
                <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    var tocbot_timer;
    var DEPTH_MAX = 6; // 为 6 时展开所有
    var tocbot_default_config = {
        tocSelector: '.tocbot-list',
        contentSelector: '.post-content',
        headingSelector: 'h1, h2, h3, h4, h5',
        orderedList: false,
        scrollSmooth: true,
        onClick: extend_click,
    };

    function extend_click() {
        clearTimeout(tocbot_timer);
        tocbot_timer = setTimeout(function() {
            tocbot.refresh(obj_merge(tocbot_default_config, {
                hasInnerContainers: true
            }));
        }, 420); // 这个值是由 tocbot 源码里定义的 scrollSmoothDuration 得来的
    }

    document.ready(function() {
        tocbot.init(obj_merge(tocbot_default_config, {
            collapseDepth: 1
        }));
    });

    function expand_toc() {
        var b = document.querySelector('.tocbot-toc-expand');
        var expanded = b.getAttribute('data-expanded');
        expanded ? b.removeAttribute('data-expanded') : b.setAttribute('data-expanded', true);
        tocbot.refresh(obj_merge(tocbot_default_config, {
            collapseDepth: expanded ? 1 : DEPTH_MAX
        }));
        b.innerText = expanded ? 'Expand all' : 'Collapse all';
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

    function obj_merge(target, source) {
        for (var item in source) {
            if (source.hasOwnProperty(item)) {
                target[item] = source[item];
            }
        }
        return target;
    }
</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">⛵ 各种环境都要能用 Git</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">kissdata</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">Nov 22, 2023&nbsp;&nbsp;0:00</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/Deploy/">Deploy</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <p>[按] 主要记录 Git 在 <code>Linux</code> 上的安装、配置和常用的选项 <a target="_blank" rel="noopener" href="https://git-scm.com/download/win"><img src="https://img.shields.io/badge/git-2.43.0-orange.svg" alt="git2"></a></p>
<h2 id="安装最新版"><a href="#安装最新版" class="headerlink" title="安装最新版"></a>安装最新版</h2><h3 id="1-1-Windows-安装"><a href="#1-1-Windows-安装" class="headerlink" title="1.1 Windows 安装"></a>1.1 Windows 安装</h3><p>直接从 <a target="_blank" rel="noopener" href="https://git-scm.com/download/win">git-scm.com</a> 下载 (58M)，然后点点点 ok。</p>
<h3 id="1-2-CentOS-安装"><a href="#1-2-CentOS-安装" class="headerlink" title="1.2 CentOS 安装"></a>1.2 CentOS 安装</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.43.0.tar.gz</span><br><span class="line"><span class="comment"># 安装 openssl 时会顺带自动安装 zlib-devel 等其他依赖库</span></span><br><span class="line">yum -y install curl-devel expat-devel openssl-devel </span><br><span class="line">tar -C /usr/local/src -zxvf git-2.4*</span><br><span class="line"><span class="built_in">cd</span> /usr/local/src/git-2.4*</span><br><span class="line">make</span><br><span class="line">make install </span><br></pre></td></tr></table></figure>

<p>自动安装到用户的 <code>bin</code> 目录 ( root 安装是在 <code>/root/bin/</code> )</p>
<h3 id="1-3-Ubuntu-安装"><a href="#1-3-Ubuntu-安装" class="headerlink" title="1.3 Ubuntu 安装"></a>1.3 Ubuntu 安装</h3><p>ubuntu有在 <a target="_blank" rel="noopener" href="https://launchpad.net/~git-core/+archive/ubuntu/ppa">PPA</a> 上维护最新版的git，不需要源码编译。</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sudo add-apt-repository ppa:git-core/ppa</span><br><span class="line">sudo apt update</span><br><span class="line">sudo apt install git -y</span><br></pre></td></tr></table></figure>

<h2 id="图形化工具"><a href="#图形化工具" class="headerlink" title="图形化工具"></a>图形化工具</h2><p>安装图形化工具，Linux平台直接 <code>apt install -y gitg</code> 。Windows 平台有图形工具 <strong>sourcetree</strong> ，这个工具好（没有Linux版），自动增加参数对于克隆大仓库，可以避免不必要的记忆。比如下载书籍资料：</p>
<p><img src="/image/tool_usage/sourcetree.png"></p>
<h2 id="本地仓库的操作"><a href="#本地仓库的操作" class="headerlink" title="本地仓库的操作"></a>本地仓库的操作</h2><h3 id="2-1-查看git配置信息"><a href="#2-1-查看git配置信息" class="headerlink" title="2.1 查看git配置信息"></a>2.1 查看git配置信息</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">git config --global -l  <span class="comment"># 显示全局</span></span><br><span class="line">git config user.name    <span class="comment"># 查看用户名</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 必须的配置</span></span><br><span class="line">git config --global user.name <span class="string">&quot;username&quot;</span></span><br><span class="line">git config --global user.email <span class="string">&quot;email&quot;</span></span><br><span class="line">git config --global http.sslVerify <span class="string">&quot;false&quot;</span></span><br><span class="line">git config --global core.autocrlf input    <span class="comment"># 自动换行</span></span><br></pre></td></tr></table></figure>

<p>注意有时的 git clone 链接是以 <code>git@</code> 开头，要换成 <code>http@</code></p>
<h3 id="2-2-新建分支开发"><a href="#2-2-新建分支开发" class="headerlink" title="2.2 新建分支开发"></a>2.2 新建分支开发</h3><p>init 新的项目或者 clone别人的项目，修改前先开个自己的分支。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">git branch           <span class="comment"># 显示所有分支, 默认master</span></span><br><span class="line">git branch wang      <span class="comment"># 自己的分支</span></span><br><span class="line">git checkout wang    <span class="comment">#</span></span><br><span class="line">git branch           <span class="comment"># 看当前分支, 是绿色的并且前面有*号</span></span><br><span class="line"></span><br><span class="line">git merge wang       <span class="comment"># 分支合并到master</span></span><br><span class="line">git branch -d wang   <span class="comment"># 删除</span></span><br></pre></td></tr></table></figure>

<p>想更新时，重复上述步骤。</p>
<h3 id="2-3-跳转分支开发"><a href="#2-3-跳转分支开发" class="headerlink" title="2.3 跳转分支开发"></a>2.3 跳转分支开发</h3><p>工作内容到一半需要去其他分支，此时如果把手里内容推送，提交的信息半成品是“污染” 记录的。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git stash</span><br><span class="line">git stash list</span><br><span class="line">git stash pop</span><br></pre></td></tr></table></figure>



<h2 id="远程仓库操作"><a href="#远程仓库操作" class="headerlink" title="远程仓库操作"></a>远程仓库操作</h2><h3 id="3-1-维护自己的仓库"><a href="#3-1-维护自己的仓库" class="headerlink" title="3.1 维护自己的仓库"></a>3.1 维护自己的仓库</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">git remote -v                  <span class="comment"># 查看远程</span></span><br><span class="line">git remote add repname         <span class="comment"># 增加一个仓库</span></span><br><span class="line">git remote <span class="built_in">rm</span> repname git@...  <span class="comment"># 删除远程</span></span><br><span class="line"></span><br><span class="line">git <span class="built_in">clone</span> git@... -b go        <span class="comment"># 直接获取go分支代码</span></span><br><span class="line">git branch -a</span><br></pre></td></tr></table></figure>

<p>版本回退：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">log</span>          <span class="comment"># 查看之前的版本号</span></span><br><span class="line">git reset xxxx   <span class="comment"># 本地回退到指定版本</span></span><br><span class="line">git stash  </span><br><span class="line">git push --force <span class="comment"># 强制推送, 这样之后的版本会删除</span></span><br><span class="line">git stash pop</span><br><span class="line">git add .        <span class="comment"># 修改后重新开始推送</span></span><br></pre></td></tr></table></figure>

<h3 id="3-2-查别人"><a href="#3-2-查别人" class="headerlink" title="3.2 查别人"></a>3.2 查别人</h3><p>下面的参数可以组合使用</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">log</span> --oneline             <span class="comment"># 历史记录简洁版</span></span><br><span class="line">git <span class="built_in">log</span> --author=name         <span class="comment"># 查看具体特定人的历史提交</span></span><br><span class="line">git <span class="built_in">log</span> --reverse             <span class="comment"># 查看首次提交</span></span><br></pre></td></tr></table></figure>



<h2 id="附：测试"><a href="#附：测试" class="headerlink" title="附：测试"></a>附：测试</h2><p>【demo 1】把自己预制的环境脚本安上</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">clone</span> --depth 1 https://gitee.com/AE85/bashenv.git</span><br><span class="line"><span class="built_in">cd</span> bashenv</span><br><span class="line">sh install2centos.sh</span><br></pre></td></tr></table></figure>

<p>【demo 2】如果有图形化界面，安装仙剑</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> /home</span><br><span class="line">git <span class="built_in">clone</span> --depth 2 https://github.com/sdlpal/sdlpal.git</span><br><span class="line"><span class="built_in">cd</span> sdlpal</span><br><span class="line">git submodule update --init --recursive</span><br><span class="line"><span class="built_in">cd</span> unix</span><br><span class="line"><span class="built_in">ln</span> -s /usr/lib64/libGL.so.1 /usr/lib/libGL.so <span class="comment"># 找不到 libGL</span></span><br><span class="line">make</span><br></pre></td></tr></table></figure>


        </div>

        
            <section class="post-copyright">
                
                
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span>人生三大错觉：我躲得过去，我能反杀，是不是<strong>喜欢我?</strong></span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2023/11-22/ability_english/">英语能力</a>
            
            
            <a class="next" rel="next" href="/2023/11-4/deploy_gocv/">🆗 | Centos7 安装openCV4</a>
            
        </section>


    </article>
</div>

            </div>
            <footer id="footer" class="footer">
    <div class="copyright">
        <span>187-9597-5517 &nbsp; 王鹏 @ <a href="https://cs.njupt.edu.cn/" target="_blank">njupt</a</span>
    </div>
</footer>

    </div>
</body>

</html>